package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.ProductInfoMapper;
import com.winshare.saleAssistant.domain.dto.req.BaseIdDTO;
import com.winshare.saleAssistant.domain.dto.req.ProductInfoCreateDTO;
import com.winshare.saleAssistant.domain.dto.req.ProductInfoQueryDTO;
import com.winshare.saleAssistant.domain.dto.req.ProductInfoUpdateDTO;
import com.winshare.saleAssistant.domain.dto.resp.ProductInfoRespDTO;
import com.winshare.saleAssistant.domain.entity.ProductInfoDO;
import com.winshare.saleAssistant.domain.query.ProductInfoQuery;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.ProductInfoMapStruct;
import com.winshare.saleAssistant.service.ProductInfoService;
import com.winshare.saleAssistant.util.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    private final ProductInfoMapper productInfoMapper;

    public ProductInfoServiceImpl(ProductInfoMapper productInfoMapper) {
        this.productInfoMapper = productInfoMapper;
    }

    @Override
    public void saveProductInfo(ProductInfoCreateDTO productInfoCreateDTO) {
        ProductInfoDO productInfoDO = ProductInfoMapStruct.INSTANCE.fromProductInfoCreateDTO(productInfoCreateDTO);
        if (checkProductExist(productInfoDO)) {
            throw new BusinessException("产品的名称或编号重复");
        }
        productInfoMapper.saveProductInfo(productInfoDO);
    }

    @Override
    public void updateProductInfo(ProductInfoUpdateDTO productInfoUpdateDTO) {
        ProductInfoDO productInfoDO = ProductInfoMapStruct.INSTANCE.fromProductInfoUpdateDTO(productInfoUpdateDTO);
        if (checkProductExist(productInfoDO)) {
            throw new BusinessException("产品的名称或编号重复");
        }
        productInfoMapper.updateProductInfo(productInfoDO);
    }

    @Override
    public ProductInfoRespDTO getProductInfoDetail(BaseIdDTO baseIdDTO) {
        ProductInfoDO productInfoDO = ProductInfoMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        ProductInfoDO productInfoDOInSql = productInfoMapper.getProductInfo(productInfoDO);
        if (null == productInfoDOInSql) {
            throw new BusinessException("当前产品不存在");
        }
        ProductInfoRespDTO productInfoRespDTO = ProductInfoMapStruct.INSTANCE.fromProductInfoDO(productInfoDOInSql);
        return productInfoRespDTO;
    }

    @Override
    public void deleteProductInfo(BaseIdDTO baseIdDTO) {
        ProductInfoDO productInfoDO = ProductInfoMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        ProductInfoDO productInfoDOInSql = productInfoMapper.getProductInfo(productInfoDO);
        if (null == productInfoDOInSql) {
            throw new BusinessException("当前产品不存在");
        }
        productInfoMapper.deleteProductInfo(productInfoDO);
    }

    @Override
    public Page<ProductInfoRespDTO> getProductInfoList(ProductInfoQueryDTO productInfoQueryDTO) {
        ProductInfoQuery productInfoQuery = ProductInfoMapStruct.INSTANCE.fromProductInfoQueryDTO(productInfoQueryDTO);
        productInfoQuery.setOffsetIndex(productInfoQueryDTO.getOffsetIndex());
        int totalCount = productInfoMapper.queryProductInfoCount(productInfoQuery);
        Integer pageIndex = productInfoQueryDTO.getPageIndex();
        Integer pageSize = productInfoQueryDTO.getPageSize();
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        }
        List<ProductInfoDO> productInfoDOList = productInfoMapper.findProductInfoListByConditions(productInfoQuery);
        List<ProductInfoRespDTO> productInfoRespDTOList = ProductInfoMapStruct.INSTANCE
                .fromProductInfoDOList(productInfoDOList);
        return new Page<>(pageIndex, pageSize, totalCount, productInfoRespDTOList);
    }

    @Override
    public List<ProductInfoDO> getProducts(ProductInfoQuery productInfoQuery) {
        return productInfoMapper.getProductInfoListByConditions(productInfoQuery);
    }

    private boolean checkProductExist(ProductInfoDO productInfoDO) {
        return productInfoMapper.countByProductName(productInfoDO) > 0 ||
                productInfoMapper.countByProductCode(productInfoDO) > 0;
    }
}
